package com.authstr.basic.web.controller;

import java.util.*;

import com.authstr.basic.exception.BaseExceptionEnum;
import com.authstr.basic.exception.ExceptionEnumInterface;
import com.authstr.basic.reqres.utils.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 控制层的超类,
 * 主要对控制层的异常进行捕获,通过接口返回或者进行其他处理
 * @author authstr
 * 2019年10月7日18:16:41
 */
@Component
public class AbstractController {

	/**
	 *重定向 关键词
	 */
	public static final String REDIRECT = "redirect:";
	/**
	 * 转发关键词
	 */
	public static final String FORWARD = "forward:";


	protected Logger log = LoggerFactory.getLogger(this.getClass());

	/**
	 * 获取request对象
	 * @return
	 */
	public HttpServletRequest getHttpServletRequest() {
		return WebUtils.getRequest();
	}

	/**
	 * 获取response对象
	 * @return
	 */
	public HttpServletResponse getHttpServletResponse() {
		return WebUtils.getResponse();
	}

	/**
	 * 从request获取指定name的值
	 * @param name
	 * @return
	 */
	public String getPara(String name) {
		return WebUtils.getRequest().getParameter(name);
	}

	/**
	 * 设置一个键值对到request参数里
	 * @param name
	 * @param value
	 */
	protected void setAttr(String name, Object value) {
		WebUtils.getRequest().setAttribute(name, value);
	}





	/**
	 * 获取[成功]的信息map
	 * @return
	 */
	public Map success() {
		return createMsg(BaseExceptionEnum.SUCCESS);
	}

	/**
	 * 通过一个消息的枚举,创建用于响应的map对象
	 * @param msg
	 * @return
	 */
	public static Map createMsg(ExceptionEnumInterface msg) {
		Map map = new HashMap();
		map.put(ControllerConstant.CODE,msg.getCode());
		map.put(ControllerConstant.MSG,msg.getMessage());
		return map;
	}

}
